import requests
from datetime import datetime

class OpenExchangeRates:
    def __init__(self, app_id):
        self.app_id = app_id
        self.url = f"https://openexchangerates.org/api/latest.json?app_id={self.app_id}&base=USD"

    def get_exchange_rates(self):
        response = requests.get(self.url)
        data = response.json()
        return data["rates"]
    
    # 获取某天的汇率
    def get_exchange_rates_on(self, date:datetime):
        url = f"https://openexchangerates.org/api/historical/{date.strftime("%Y-%m-%d")}.json?app_id={self.app_id}&base=USD"
        response = requests.get(url)
        data = response.json()
        return data["rates"]



if __name__ == "__main__":
    app_id = "587a75de82be47da981c67ac703ef118"
    currency = OpenExchangeRates(app_id)
    rates = currency.get_exchange_rates()
    print(rates)